package com.huabo.audit.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.hbfk.config.SystemStaticValue;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblOrganizationUtil;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.enums.ProcessVariableEnum;
import com.hbfk.util.DateUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.JudgeRoleRight;
import com.hbfk.util.PageInfo;
import com.hbfk.util.ResponseFormat;
import com.huabo.audit.enums.ProcessEnum;
import com.huabo.audit.oracle.entity.TblAduitProGramEntity;
import com.huabo.audit.oracle.entity.TblAttachment;
import com.huabo.audit.oracle.entity.TblAuditOption;
import com.huabo.audit.oracle.entity.TblCirculation;
import com.huabo.audit.oracle.entity.TblNbsjAuditplan;
import com.huabo.audit.oracle.entity.TblNbsjAuthorizationEntity;
import com.huabo.audit.oracle.entity.TblNbsjOperateEntity;
import com.huabo.audit.oracle.entity.TblNbsjProject;
import com.huabo.audit.oracle.entity.TblNbsjProjectDataEntity;
import com.huabo.audit.oracle.entity.TblNbsjProjectTeamEntity;
import com.huabo.audit.oracle.entity.TblNbsjSheetReportEntity;
import com.huabo.audit.oracle.entity.TblNbsjStaffSelect;
import com.huabo.audit.oracle.entity.TblNbsjTeamstaffEntity;
import com.huabo.audit.oracle.entity.TblNbsjTempleteEntity;
import com.huabo.audit.oracle.entity.TblOrganization;
import com.huabo.audit.oracle.entity.TblStaff;
import com.huabo.audit.oracle.entity.TblTargetTypeEntity;
import com.huabo.audit.oracle.mapper.TblAduitProGramMapper;
import com.huabo.audit.oracle.mapper.TblAttachmentMapper;
import com.huabo.audit.oracle.mapper.TblCirculationMapper;
import com.huabo.audit.oracle.mapper.TblNbsjAuditplanMapper;
import com.huabo.audit.oracle.mapper.TblNbsjAuthorizationMapper;
import com.huabo.audit.oracle.mapper.TblNbsjOperateMapper;
import com.huabo.audit.oracle.mapper.TblNbsjProjectDataMapper;
import com.huabo.audit.oracle.mapper.TblNbsjProjectMapper;
import com.huabo.audit.oracle.mapper.TblNbsjProjectteamMapper;
import com.huabo.audit.oracle.mapper.TblNbsjQuestionMapper;
import com.huabo.audit.oracle.mapper.TblNbsjStaffSelectMapper;
import com.huabo.audit.oracle.mapper.TblNbsjTeamstaffMapper;
import com.huabo.audit.oracle.mapper.TblNbsjTempleteMapper;
import com.huabo.audit.oracle.mapper.TblStaffMapper;
import com.huabo.audit.oracle.mapper.TblTargetTypeMapper;
import com.huabo.audit.oracle.vo.TblGkProjectVo;
import com.huabo.audit.oracle.vo.TblGkQuestionVo;
import com.huabo.audit.oracle.vo.TblGkZgQuestionVo;
import com.huabo.audit.oracle.vo.TblnbsjProjectRwfpVo;
import com.huabo.audit.oracle.vo.TblnbsjProjectVo;
import com.huabo.audit.oracle.vo.TblnbsjProjectZXYLVo;
import com.huabo.audit.service.ActivityPluginsService;
import com.huabo.audit.service.ProcessService;
import com.huabo.audit.service.TblAduitProGramService;
import com.huabo.audit.service.TblAuditOptionService;
import com.huabo.audit.service.TblNbsjAuthorizationService;
import com.huabo.audit.service.TblNbsjProjectService;
import com.huabo.audit.service.TblNbsjProjectteamService;
import com.huabo.audit.service.TblNbsjTempleteService;
import com.huabo.audit.service.TblStaffNewService;
import com.huabo.audit.service.TblTargetTypeService;
import com.huabo.audit.util.R;

/**
 * 描述:审计项目ID实现类
 *
 * @author: ziyao
 * @date: 2022-04-12
 */
@Service
public class TblNbsjProjectServiceImpl implements TblNbsjProjectService {

	@Resource
	private TblNbsjProjectMapper tblNbsjProjectMapper;
	
	@Resource
	private TblNbsjProjectteamMapper tblNbsjProjectteamMapper;
	
	@Resource
	private ActivityPluginsService activityPluginsService;
	
	@Resource
	private TblNbsjStaffSelectMapper tblNbsjStaffSelectMapper;
	
    @Resource
   	public TblCirculationMapper tblCirculationMapper;
    
    @Resource
	private RuntimeService runtimeService;
	@Resource 
	private TaskService taskService;
	
    @Resource
    private ProcessService processService;
    
    @Resource
    private TblAuditOptionService tblAuditOptionService;
    
    @Resource
	private TblAttachmentMapper tblAttachmentMapper;
    
    @Resource
    private TblAduitProGramMapper tblAduitProGramMapper;
    
    @Resource
    private TblNbsjTeamstaffMapper tblNbsjTeamstaffMapper;
    
    @Resource
    private TblNbsjAuthorizationService tblNbsjAuthorizationService;
    
    @Resource
    private TblNbsjOperateMapper tblNbsjOperateMapper;
    
    @Resource
    private TblNbsjQuestionMapper tblNbsjQuestionMapper;
    
    @Resource
    private TblNbsjProjectDataMapper tblNbsjProjectDataMapper;
    
    @Resource
    private TblNbsjProjectService tblNbsjProjectService;
    
    @Resource
    private TblNbsjTempleteService tblNbsjTempleteService;
    
    @Resource
    private TblNbsjTempleteMapper tblNbsjTempleteMapper;
    
    @Resource
    private TblTargetTypeService tblTargetTypeService;
    
    @Resource
    private TblAduitProGramService tblAduitProGramService;
    
    @Resource
    private TblNbsjAuthorizationMapper tblNbsjAuthorizationMapper;
    
    @Resource
    private TblStaffNewService tblStaffNewService;
    
    @Resource
    private TblStaffMapper tblStaffMapper;
    
    @Resource
    private TblNbsjAuditplanMapper tblNbsjAuditplanMapper;
    
    @Resource
    private TblNbsjProjectteamService tblNbsjProjectteamService;
    
    @Resource
    private TblTargetTypeMapper tblTargetTypeMapper;
    
    @Override
	public JsonBean getNbsjProjecGdtPageList(String token, Integer pageNumber, Integer pageSize, TblnbsjProjectVo project,
			String projectStartDate, String projectEndDate) throws Exception {
		
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	
    	PageInfo<TblNbsjProject> pageInfo = new PageInfo<TblNbsjProject>();
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
    	//project.setOrgId(loginStaff.getCurrentOrg().getOrgid().intValue());
    	//project.setStaffId(loginStaff.getStaffid().intValue());
        
        pageInfo.setTlist(this.tblNbsjProjectMapper.selectProjectgdListByPageInfo(pageInfo,projectStartDate,projectEndDate,project));
        pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectProjectgdListByPageCount(projectStartDate,projectEndDate,project));
        resultMap.put("pageInfo", pageInfo);
        
		return ResponseFormat.retParam(1,200,resultMap);
		  
	}

    
    
	
	//获取当前实施项目；
		@Override
		public TblNbsjProject getCurrenNbsjProjectByLoginStaff(BigDecimal staffid) throws Exception {
			Integer projectId = this.tblNbsjStaffSelectMapper.selectProjectIdByStaffId(staffid);
			if(projectId == null) {
				return null;
			}
			return this.tblNbsjProjectMapper.selectPJById(projectId);
		}
	
	@Override
	public JsonBean getAuditFileInfoList(String token, String projectName, Integer pageNumber, Integer pageSize)
			throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
		
    	PageInfo<TblNbsjProject> pageInfo = new PageInfo<TblNbsjProject>();
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
    	
    	pageInfo.setTlist(this.tblNbsjProjectMapper.selectAuditFileListPageInfo(pageInfo,projectName,loginStaff.getCurrentOrg().getOrgid()));
    	pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectAuditFileCountByPage(pageInfo,projectName,loginStaff.getCurrentOrg().getOrgid()));
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	resultMap.put("pageInfo", pageInfo);
    	return ResponseFormat.retParam(1,200,resultMap);
	}

	
	@Override
	public JsonBean getNbsjProjectPageList(String token, Integer pageNumber, Integer pageSize, TblnbsjProjectVo project,
			String projectStartDate, String projectEndDate) throws Exception {
		
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	
    	PageInfo<TblNbsjProject> pageInfo = new PageInfo<TblNbsjProject>();
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
    	project.setOrgId(loginStaff.getCurrentOrg().getOrgid().intValue());
        if(!JudgeRoleRight.judgeRoleRight(SystemStaticValue.SJLBQX,loginStaff.getRoleNames())){
        	project.setStaffId(loginStaff.getStaffid().intValue());
        }
        
        pageInfo.setTlist(this.tblNbsjProjectMapper.selectProjectListByPageInfo(pageInfo,projectStartDate,projectEndDate,project));
        pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectProjectCountByPageInfo(pageInfo,projectStartDate,projectEndDate,project));
        
        TblNbsjStaffSelect oldtblNbsjStaffSelect = this.tblNbsjStaffSelectMapper.selectByUserId(loginStaff.getStaffid());
        
        String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_XMGL.name());
        
        resultMap.put("currentProjectid", oldtblNbsjStaffSelect.getProjectId());
        resultMap.put("identifier", identifier);
        resultMap.put("pageInfo", pageInfo);
        
		return ResponseFormat.retParam(1,200,resultMap);
		  
	}

	@Override
	 public JsonBean projectProposalPageList(String token, Integer pageNumber, Integer pageSize,TblnbsjProjectVo tblnbsjProjectVo) throws Exception {
		  TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		  if(loginStaff == null) {
		   return ResponseFormat.retParam(0,20006,null);
		  }
	  
	     if(pageNumber == null) {
	      pageNumber = 1;
	     }
	     if(pageSize==null) {
	      pageSize=15;
	     }
	     Map<String,Object> resultMap = new HashMap<String,Object>(0);
	     
	   //==查询当前实施的项目！
		TblNbsjProject tnp = this.tblNbsjProjectService.getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
		if(tnp == null) {
			return ResponseFormat.retParam(0,30003,null);
		}
		Integer projectId = tnp.getProjectId();
		if(null == projectId) {
			return ResponseFormat.retParam(0,30003,null);
		}
		tblnbsjProjectVo.setProjectId(projectId);
	     
	     com.huabo.audit.util.PageInfo<TblNbsjProject> pageInfo = new com.huabo.audit.util.PageInfo<TblNbsjProject>();
//	     tblNbsjSheet.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//	     pageInfo.setCondition(tblNbsjSheet);
	     pageInfo.setPageSize(pageSize);
	     pageInfo.setCurrentPage(pageNumber);
	     pageInfo.setTlist(this.tblNbsjProjectMapper.selectListByPageInfo(pageInfo,tblnbsjProjectVo));
	     pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectCountByPageInfo(pageInfo,tblnbsjProjectVo));
	     pageInfo.getTotalPage();
	     String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
	     resultMap.put("identifier", identifier);
	     resultMap.put("pageInfo", pageInfo);
	     return ResponseFormat.retParam(1,200,resultMap);
	 }

	 @Override
	 public JsonBean findProjectProposalDetail(String token, Integer dataId) throws Exception {
		  TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		  if(loginStaff == null) {
		   return ResponseFormat.retParam(0,20006,null);
		  }
		  Map<String,Object> resultMap = new HashMap<String,Object>(0);
		  
		  TblNbsjProject plan = this.tblNbsjProjectMapper.selectById(dataId);
		  resultMap.put("Project", plan);
		  return ResponseFormat.retParam(1,200,resultMap);
	 }


	@Override
	public JsonBean getAuditFileDetailTreeInfo(String token, Integer projectId) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		/*TblNbsjProject project = null;
        if (projectId != null) {
            project = tblNbsjProjectMapper.selectEntityById(projectId);
        }else {
        	project =  this.tblNbsjProjectMapper.getSelectProject();
        }
		IF()*/
		
		
		
		return null;
	}


	@Override
	public JsonBean sjgdNewPageList(String token, Integer pageNumber, Integer pageSize,
			TblnbsjProjectVo tblnbsjProjectVo) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		  if(loginStaff == null) {
		   return ResponseFormat.retParam(0,20006,null);
		  }
	  
	     if(pageNumber == null) {
	      pageNumber = 1;
	     }
	     if(pageSize==null) {
	      pageSize=15;
	     }
	     Map<String,Object> resultMap = new HashMap<String,Object>(0);
	     com.huabo.audit.util.PageInfo<TblNbsjProject> pageInfo = new com.huabo.audit.util.PageInfo<TblNbsjProject>();
//	     tblNbsjSheet.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//	     pageInfo.setCondition(tblNbsjSheet);
	     pageInfo.setPageSize(pageSize);
	     pageInfo.setCurrentPage(pageNumber);
	        //审计责任人、审计负责人可以看所有归档的项目
	        if(loginStaff.getTrole()!=null&&(loginStaff.getTrole().getRname().equals("审计部负责人")||loginStaff.getTrole().getRname().equals("审计责任人"))) {
	        	pageInfo.setTlist(this.tblNbsjProjectMapper.selectSjgdNewListByPageInfo(pageInfo,tblnbsjProjectVo));
	        	pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectSjgdNewCountByPageInfo(pageInfo,tblnbsjProjectVo));
	        }else {
	        	//项目经理看自己的项目
	        	//工作人员看借阅的项目
	   	       tblnbsjProjectVo.setStaffId(Integer.parseInt(loginStaff.getStaffid().toString()));	 
	        	pageInfo.setTlist(this.tblNbsjProjectMapper.selectSjgdNewListByStaffidPageInfo(pageInfo,tblnbsjProjectVo));
	        	pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectSjgdNewCountByStaffidPageInfo(pageInfo,tblnbsjProjectVo));
	        }
	     pageInfo.getTotalPage();
	     String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
	     resultMap.put("identifier", identifier);
	     resultMap.put("pageInfo", pageInfo);
	     return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean findSjgdNewDetail(String token, Integer projectid) throws Exception {
		 TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		  if(loginStaff == null) {
		   return ResponseFormat.retParam(0,20006,null);
		  }
		  Map<String,Object> resultMap = new HashMap<String,Object>(0);
		  
		  TblNbsjProject plan = this.tblNbsjProjectMapper.selectById(projectid);
		  resultMap.put("Project", plan);
		  return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean dajyNewPageList(String token, Integer pageNumber, Integer pageSize) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		  if(loginStaff == null) {
		   return ResponseFormat.retParam(0,20006,null);
		  }
	  
	     if(pageNumber == null) {
	      pageNumber = 1;
	     }
	     if(pageSize==null) {
	      pageSize=15;
	     }
	     Map<String,Object> resultMap = new HashMap<String,Object>(0);
	     
	     BigDecimal staffid = loginStaff.getStaffid();
	     BigDecimal orgid = loginStaff.getCurrentOrg().getOrgid();
	     
	     com.huabo.audit.util.PageInfo<TblNbsjProject> pageInfo = new com.huabo.audit.util.PageInfo<TblNbsjProject>();
//	     tblNbsjSheet.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//	     pageInfo.setCondition(tblNbsjSheet);
	     pageInfo.setPageSize(pageSize);
	     pageInfo.setCurrentPage(pageNumber);
	     pageInfo.setTlist(this.tblNbsjProjectMapper.selectDajyNewListByPageInfo(pageInfo,orgid,staffid));
	     pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectDajyNewCountByPageInfo(pageInfo,orgid,staffid));
	     pageInfo.getTotalPage();
	     String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
	     resultMap.put("identifier", identifier);
	     resultMap.put("pageInfo", pageInfo);
	     return ResponseFormat.retParam(1,200,resultMap);
	}


	@Override
	public JsonBean jyrzNewPageList(String token, Integer pageNumber, Integer pageSize) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		  if(loginStaff == null) {
		   return ResponseFormat.retParam(0,20006,null);
		  }
		  
		  Integer orgid = loginStaff.getCurrentOrg().getOrgid().intValue();
	  
	     if(pageNumber == null) {
	      pageNumber = 1;
	     }
	     if(pageSize==null) {
	      pageSize=15;
	     }
	     Map<String,Object> resultMap = new HashMap<String,Object>(0);
	     
	     com.huabo.audit.util.PageInfo<TblNbsjProject> pageInfo = new com.huabo.audit.util.PageInfo<TblNbsjProject>();
//	     tblNbsjSheet.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//	     pageInfo.setCondition(tblNbsjSheet);
	     pageInfo.setPageSize(pageSize);
	     pageInfo.setCurrentPage(pageNumber);
	     pageInfo.setTlist(this.tblNbsjProjectMapper.selectJyrzNewListByPageInfo(pageInfo,orgid,Integer.parseInt(loginStaff.getStaffid().toString())));
	     pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectJyrzNewCountByPageInfo(pageInfo,orgid));
	     pageInfo.getTotalPage();
	     String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
	     resultMap.put("identifier", identifier);
	     resultMap.put("pageInfo", pageInfo);
	     return ResponseFormat.retParam(1,200,resultMap);
	}


	//==
	@Override
	public JsonBean projectPageList(String token, Integer pageNumber, Integer pageSize,
			TblnbsjProjectVo tblnbsjProjectVo) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		TblOrganizationUtil t1 = loginStaff.getCurrentOrg();
		tblnbsjProjectVo.setOrgId(t1.getOrgid().intValue());
		tblnbsjProjectVo.setStaffId(loginStaff.getStaffid().intValue());
		if(loginStaff.getTrole()!=null) {
			tblnbsjProjectVo.setRolename(loginStaff.getTrole().getRname());
		}
		
		
		
		
		
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	
    	PageInfo<TblNbsjProject> pageInfo = new PageInfo<TblNbsjProject>();
//    	tblNbsjWorkReport.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//    	pageInfo.setCondition(tblNbsjWorkReport);
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
    	pageInfo.setTlist(this.tblNbsjProjectMapper.selectPJListByPageInfo(pageInfo,tblnbsjProjectVo));
    	pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectPJCountByPageInfo(pageInfo,tblnbsjProjectVo));
    	pageInfo.getTotalPage();
    	String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
    	resultMap.put("identifier", identifier);
    	resultMap.put("pageInfo", pageInfo);
    	
    	//==查询当前实施的项目！
		TblNbsjProject tnp = this.getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
		if(tnp != null) {
			Integer projectId = tnp.getProjectId();
	    	resultMap.put("currProjectId", projectId);
		}
		
    	
    	return ResponseFormat.retParam(1,200,resultMap);
	}


	@Override
	public JsonBean projectAdd(TblNbsjProject pj, String token,String planStartDate,String planEndDate,String attids,String pd_dx,String pjTeamJson) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		pj.setPlanStartDate(planStartDate);
		pj.setPlanEndDate(planEndDate);
		
		Integer count = this.tblNbsjProjectMapper.selectPlanCodeByOrgid(pj);
		if(count > 0) {
			return ResponseFormat.retParam(0,202,null);
		}
		
		String belongsto = "";
		if (StringUtils.isNotEmpty(pd_dx) && pd_dx.equals("yh")){
			belongsto = pj.getAuditStaffId()+"";
		}else {
			belongsto = pj.getAuditOrgId()+"";
		}
		xmlbUpdate(pj, token, pj.getPmId()+"", planEndDate, planStartDate, pj.getTempzyId()+"", pj.getTempId()+"",
				pj.getProtempid()+"", pd_dx, belongsto, pj.getPlanId()+"",attids,pjTeamJson);
		
		
//		Integer orgid = loginStaff.getCurrentOrg().getOrgid().intValue();
//		pj.setOrgId(orgid);
//    	
//		pj.setCreateStaffId(loginStaff.getStaffid().intValue());
//		pj.setCreateTime(new Date());
//		pj.setStatus(0);
//		//根据planId主键是否为空判断新增还是修改 ，主键为空新增、不为空修改；
//		
//		if(pj.getProjectId() != null) {
//			//修改；
//			this.tblNbsjProjectMapper.updateEntity(pj);
//			//==附件，先删除 再重新添加
//			this.tblAttachmentMapper.deleteAttmentRelationProject(pj.getProjectId());
//			if (attids != null && !"".equals(attids)) {
//				String[] ids = attids.split(",");
//				for (String id : ids) {
//					this.tblAttachmentMapper.insertAttmentRelationProject(id, pj.getProjectId());
//				}
//			}
//		}else {
//			//新增；
//			this.tblNbsjProjectMapper.insertEntity(pj);
//			//==附件
//			if (attids != null && !"".equals(attids)) {
//				String[] ids = attids.split(",");
//				for (int i = 0; i < ids.length; i++) {
//					String id = ids[i];
//					this.tblAttachmentMapper.insertAttmentRelationProject(id, pj.getProjectId());
//				}
//			}
//		}
		
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		resultMap.put("WorkReport",pj);
		return ResponseFormat.retParam(1,200,resultMap);
	}


	@Override
	public JsonBean projectDelete(Integer projectid, String token) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		TblNbsjProject plan = this.tblNbsjProjectMapper.selectById(projectid);
		
		if(plan == null) {
			return ResponseFormat.retParam(0,50001,null);
		}
		
		if (plan.getExamineType().equals(TblNbsjProject.EXAMINETYPE1)) {
			this.tblNbsjProjectMapper.deleteById(projectid);
			
			//删除小组
			this.tblNbsjProjectMapper.deleteLinkTeamById(projectid);
			
			//删除模板
			
			return ResponseFormat.retParam(1,200,null);
        } else {
            return ResponseFormat.retParam(0,50001,null);
        }
		
		
	}


	@Override
	public JsonBean findProjectDetail(String token,Integer projectid) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		
		
		if(null == projectid) {
			TblNbsjProject tnp = this.getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
			if(tnp == null) {
				return ResponseFormat.retParam(0,10001,resultMap);
			}
			
			projectid = tnp.getProjectId();
		}
		
		
		TblNbsjProject plan = this.tblNbsjProjectMapper.selectPJById(projectid);
		
		Integer aorgid = plan.getAuditOrgId();
//		Integer astaffid = plan.getAuditStaffId();
		
		if(null == aorgid) {
			plan.setIsBmAudit(0);
		}else {
			plan.setIsBmAudit(1);
		}
		
		resultMap.put("pj", plan);
		return ResponseFormat.retParam(1,200,resultMap);
	}

	
	@Override
	public JsonBean projectRwfpPageList(String token, Integer pageNumber, Integer pageSize,
			TblnbsjProjectRwfpVo tblnbsjProjectRwfpVo) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		TblOrganizationUtil t1 = loginStaff.getCurrentOrg();
		tblnbsjProjectRwfpVo.setOrgId(t1.getOrgid().intValue());
		
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	
    	tblnbsjProjectRwfpVo.setPmId(loginStaff.getStaffid().intValue());
    			
    	PageInfo<TblNbsjProject> pageInfo = new PageInfo<TblNbsjProject>();
//    	tblNbsjWorkReport.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//    	pageInfo.setCondition(tblNbsjWorkReport);
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
    	pageInfo.setTlist(this.tblNbsjProjectMapper.selectPJRwfpListByPageInfo(pageInfo,tblnbsjProjectRwfpVo));
    	pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectPJRwfpCountByPageInfo(pageInfo,tblnbsjProjectRwfpVo));
    	pageInfo.getTotalPage();
    	String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
    	resultMap.put("identifier", identifier);
    	resultMap.put("pageInfo", pageInfo);
    	return ResponseFormat.retParam(1,200,resultMap);
	}


	@Override
	public JsonBean projectZxylPageList(String token, Integer pageNumber, Integer pageSize,
			TblnbsjProjectZXYLVo tblnbsjProjectZXYLVo) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		TblOrganizationUtil t1 = loginStaff.getCurrentOrg();
		tblnbsjProjectZXYLVo.setOrgId(t1.getOrgid().intValue());
		
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	
    	PageInfo<TblNbsjProject> pageInfo = new PageInfo<TblNbsjProject>();
//    	tblNbsjWorkReport.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//    	pageInfo.setCondition(tblNbsjWorkReport);
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
    	pageInfo.setTlist(this.tblNbsjProjectMapper.selectPJZxylListByPageInfo(pageInfo,tblnbsjProjectZXYLVo));
    	pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectPJZxylCountByPageInfo(pageInfo,tblnbsjProjectZXYLVo));
    	pageInfo.getTotalPage();
    	String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
    	resultMap.put("identifier", identifier);
    	resultMap.put("pageInfo", pageInfo);
    	return ResponseFormat.retParam(1,200,resultMap);
	}


	@Override
	public JsonBean findSSProjectDetail(String token,Integer projectid) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		
		if(null == projectid) {
			//==查询当前实施的项目！
			TblNbsjProject tnp = this.getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
			if(tnp == null) {
				return ResponseFormat.retParam(0,30003,resultMap);
			}
			projectid = tnp.getProjectId();
		}
		
		
		TblNbsjProject plan = this.tblNbsjProjectMapper.selectPJById(projectid);
		
		Integer count = this.tblNbsjOperateMapper.getCount(projectid);
		Integer questionCount = this.tblNbsjQuestionMapper.getQuestionCount(projectid);
		Integer staffCount = this.tblNbsjTeamstaffMapper.getCountByProject(projectid);
		
		resultMap.put("pj", plan);
		resultMap.put("sjtrry", staffCount);
		resultMap.put("jcqd", count);
		resultMap.put("fawt", questionCount);
		
		return ResponseFormat.retParam(1,200,resultMap);
	}


	@Override
	public JsonBean projectSS(String token, Integer projectid) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		TblNbsjProject project = this.tblNbsjProjectMapper.getById(projectid+"");
		
		
		//当前实施项目
		TblNbsjProject tnp = this.getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
		if(null != tnp) {
			Integer curr_projectid = tnp.getProjectId();
			if(projectid.equals(curr_projectid)) {
				JsonBean json = new JsonBean(0, "当前项目已实施！", null);
				return json;
			}
		}
		
		if (null != project && null!=project.getUpdateStatus() && project.getUpdateStatus().equals(TblNbsjProject.UPDATEYES)) {
            if (project.getStatus() != null && project.getStatus() == 0) {
                return ResponseFormat.retParam(0,80001,null);
            } else if (null != project.getStatus() && project.getStatus().equals(3) && project.getStatus().equals(4)) {
                return ResponseFormat.retParam(0,80002,null);
            } else {
            	if (null == project.getImplementTime()) {
                    project.setImplementTime(new Date());
                }
            	this.tblNbsjStaffSelectMapper.deleteNbsjByStaff(loginStaff.getStaffid());
            	this.tblNbsjStaffSelectMapper.insertEntity(loginStaff.getStaffid(),project.getProjectId());
            	this.tblNbsjProjectMapper.updateImplementTime(project.getProjectId(),DateUtil.parseDate(project.getImplementTime(),"yyyy-MM-dd HH:mm:ss"));
        		return ResponseFormat.retParam(1,200,null);
            }
        } else {
        	return ResponseFormat.retParam(0,80003,null);
        }
		
		
	}


	@Override
	public TblNbsjProject getSSProjectDetail(String token) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return null;
		}
		//==查询当前实施的项目！
		TblNbsjProject tnp = this.getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
		if(tnp != null) {
			return tnp;
		}
		
		return  null;
		
	}


	@Override
	public TblNbsjProject getProjectById(Integer projectid) throws Exception {
		TblNbsjProject plan = this.tblNbsjProjectMapper.selectPJById(projectid);
		return plan;
	}


	@Override
	public JsonBean pjPmModi(String token, Integer pmId, Integer projectid) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		//修改；
		this.tblNbsjProjectMapper.updatePjPm(pmId,projectid);
			
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
//		resultMap.put("WorkReport",pj);
		return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean pjStart(String token, Integer projectid) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		//修改；
		this.tblNbsjProjectMapper.updatePjStart(projectid);
			
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
//		resultMap.put("WorkReport",pj);
		return ResponseFormat.retParam(1,200,resultMap);
	}
	@Override
	public JsonBean submitProjectArrpoval(String token, Integer projectId) throws Exception {
		TblStaffUtil user = DealUserToken.parseUserToken(token);
		if(user == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
    	TblNbsjProject project = tblNbsjProjectMapper.selectById(projectId);
		if(project == null) {
			return ResponseFormat.retParam(0,50002,null);
		}
        TblCirculation cir = new TblCirculation();
        try {
           cir.setCytype(TblCirculation.TYPE_XMSP);
           cir.setCycode(project.getProjectCode());
           cir.setCyname(project.getPrjoectName());
           cir.setCydate(new Date());
           cir.setCystate(TblCirculation.STATE_FQ);
           cir.setCyurl(TblCirculation.URL_XMSP+projectId);
           cir.setCyStaffid(user.getStaffid().toString());
           this.tblCirculationMapper.saveTblCirculation(cir);
           
           Map<String, Object> variables= new HashMap<String, Object>();
           variables.put(ProcessVariableEnum.model.toString(), cir);
           ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(ProcessEnum.SJ_XMGL.name(),cir.getCyid().toString(), variables);
           Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
           cir.setTaskid(task.getId());
           //流程定义Id
           String businessKey = processInstance.getBusinessKey();
           String definitionId = processInstance.getProcessDefinitionId();
           cir.setBusinesskey(businessKey);
           cir.setDefinitionid(definitionId);
           this.tblCirculationMapper.updateCirculationInfoById(cir);
           taskService.complete(task.getId());
           project.setExamineType(TblNbsjProject.EXAMINETYPE2);
           tblNbsjProjectMapper.updateEntity(project);
        } catch (Exception e) {
        	e.printStackTrace();
        	if(cir != null && cir.getCyid() != null) {
        		this.tblCirculationMapper.deleteEntityById(cir.getCyid());
        	}
        	return ResponseFormat.retParam(1,30002,null);
        }	
        return ResponseFormat.retParam(1,200,null);
	}

	@Override
	public JsonBean getProjectApprovalInfo(String token, Integer projectId, String taskId,Integer cyId) throws Exception {
		TblStaffUtil staff = DealUserToken.parseUserToken(token);
		if(staff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		
		
    	TblNbsjProject project = tblNbsjProjectMapper.selectById(projectId);
        List<String> btnList = null;
    	if(project!=null){
    		if(StringUtils.isNotBlank(taskId) && project.getExamineType() != TblNbsjProject.EXAMINETYPE5){
    			btnList = processService.getButtonsForTransition(taskId);
    			resultMap.put("btnList", btnList);
    		}
    	
    		List<TblAuditOption> ao = tblAuditOptionService.findOptionByRelationId(projectId.toString(),cyId);
    		if (cyId != null){
    			TblCirculation cy = this.tblCirculationMapper.findById(cyId.toString());
    			resultMap.put("cy", cy);
    		}
    		
    		resultMap.put("taskId", taskId);
    		resultMap.put("aoptionList", ao);
    		resultMap.put("cyId", cyId);
    		resultMap.put("project", project);
    	}
    	return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean dealProjectApporvalInfo(String token, Integer cyId, String taskId, String transition,
			String optDesc, String projectId) throws Exception {
		TblStaffUtil staff = DealUserToken.parseUserToken(token);
		if(staff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		JsonBean jsonBean = null;
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		
		TblCirculation cy = this.tblCirculationMapper.findById(cyId.toString());
		if (null != cy) {
			Map<String, Object> variables = new HashMap<>();
			variables.put(ProcessVariableEnum.model.toString(), cy);
			if (StringUtils.isNotBlank(transition)) {
				variables.put(ProcessVariableEnum.transition.toString(), transition);
			}
			try {
				taskService.claim(taskId, staff.getStaffid().toString());
				taskService.complete(taskId, variables);
			} catch (Exception e) {
				e.printStackTrace();
				return ResponseFormat.retParam(0,30002,null);
			}
			//添加审批意见
			TblAuditOption opt = new TblAuditOption();
			opt.setCyid(cy.getCyid());
			opt.setOptStaffid(staff.getStaffid());
			opt.setStaffidName(staff.getRealname());
			opt.setOptDesc(optDesc);
			opt.setRelationId(new BigDecimal(projectId));
			opt.setOptState(transition);
			jsonBean = this.tblAuditOptionService.saveAuditOptionInfo(cy,opt);
		}
		return jsonBean;
	}

	@Override
	public JsonBean jsfpRoleManageSave(String token, String ids, Integer teamId, Integer projectId) throws Exception {
		TblNbsjProject project = this.tblNbsjProjectMapper.selectPJById(projectId);//this.tblnbsjProjectService.getId(projectId);
        TblNbsjTeamstaffEntity teamStaff = this.tblNbsjTeamstaffMapper.selectById(teamId);//this.tblNbsjTeamstaffService.get(teamId);
        List<TblNbsjAuthorizationEntity> list = new ArrayList<TblNbsjAuthorizationEntity>();
        List<TblNbsjAuthorizationEntity> updatelist = new ArrayList<TblNbsjAuthorizationEntity>();
        
        TblStaffUtil staff = DealUserToken.parseUserToken(token);
		if(staff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		TblStaff tblStaff = new TblStaff();
		tblStaff.setStaffid(staff.getStaffid());
        
        if (null != project && null != teamStaff) {
            String[] str_ids = ids.split(",");
            for (String str : str_ids) {
            	
//                TblAduitProGramEntity aduitProGram = this.tblAduitProGramService.get(new BigDecimal(str));
            	TblAduitProGramEntity aduitProGram = this.tblAduitProGramMapper.selectById(Integer.valueOf(str));
//                TblNbsjAuthorizationEntity authorization = this.tblNbsjAuthorizationService.get(new BigDecimal(projectId), aduitProGram.getProgramId());、
            	TblNbsjAuthorizationEntity authorization = this.tblNbsjAuthorizationService.get(projectId, aduitProGram.getProgramId());
				if (null != authorization) {
                    authorization.setAduitProGram(aduitProGram);
                    authorization.setAuthTime(new Date());
                    authorization.setProject(project);
                    authorization.setTeamStaff(teamStaff);
                    authorization.setAuthStaff(tblStaff);
                    updatelist.add(authorization);
                } else {
                    authorization = new TblNbsjAuthorizationEntity();
                    authorization.setAduitProGram(aduitProGram);
                    authorization.setAuthTime(new Date());
                    authorization.setProject(project);
                    authorization.setTeamStaff(teamStaff);
                    authorization.setAuthStaff(tblStaff);
                    list.add(authorization);
                }
            }
            this.tblNbsjAuthorizationService.merge(updatelist);
            this.tblNbsjAuthorizationService.save(list);
//            this.tblNbsjProjectMapper.updateEntity(project);
//            tblnbsjProjectService.update(project);
            //全部分配完成
        	Integer tempId = project.getTbltemplete().getTempleteId();//审计模板id
        	List<TblAduitProGramEntity> listTap = this.tblAduitProGramMapper.findByTMId(tempId);
        	boolean is = false;
        	if (listTap.size()>=0){
        			List<TblNbsjAuthorizationEntity> byProjectId = tblNbsjAuthorizationService.getByProjectId(projectId);
        			if (byProjectId.size()== listTap.size()) {
        				is = true;
        			}else {
        				is = false;
        			}
        	}
        	if(is==true) {
                //项目分配中
//                project.setFpStatus(2);//已分配
                this.tblNbsjProjectMapper.updateFpStatus(2,projectId);
        	}else {
                //项目分配中
//                project.setFpStatus(1);//分配中
                this.tblNbsjProjectMapper.updateFpStatus(1,projectId);
        	}
            
            Map<String,Object> resultMap = new HashMap<String,Object>(0);
            return ResponseFormat.retParam(1,200,resultMap);
        }
        return ResponseFormat.retParam(0,30001,null);
	}

	@Override
	public JsonBean auditPlanListPlanIdIn(String token, Integer pageNumber, Integer pageSize,String projectname) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		  if(loginStaff == null) {
		   return ResponseFormat.retParam(0,20006,null);
		  }
	  
	     if(pageNumber == null) {
	      pageNumber = 1;
	     }
	     if(pageSize==null) {
	      pageSize=15;
	     }
	     
	     BigDecimal orgid = loginStaff.getCurrentOrg().getOrgid();
	     
//	     TblNbsjProject sspj = getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
	     
	     Map<String,Object> resultMap = new HashMap<String,Object>(0);
	     
	     com.huabo.audit.util.PageInfo<TblNbsjProject> pageInfo = new com.huabo.audit.util.PageInfo<TblNbsjProject>();
//	     tblNbsjSheet.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//	     pageInfo.setCondition(tblNbsjSheet);
	     pageInfo.setPageSize(pageSize);
	     pageInfo.setCurrentPage(pageNumber);
	     pageInfo.setTlist(this.tblNbsjProjectMapper.selectAuditPlanListByPageInfo(pageInfo,projectname,orgid));
	     pageInfo.setTotalRecord(this.tblNbsjProjectMapper.selectAuditPlanCountByPageInfo(pageInfo,projectname,orgid));
	     pageInfo.getTotalPage();
	     String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
	     resultMap.put("identifier", identifier);
	     resultMap.put("pageInfo", pageInfo);
	     return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean auditPlanInAdd(String ids, String token) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		
		if (StringUtils.isNotBlank(ids)) {
            String[] id = ids.split(",");
            
            TblNbsjProject selectProject = getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
            
            if (null != selectProject) {
                for (String string : id) {
                	TblNbsjProject project = this.tblNbsjProjectMapper.selectPJById(Integer.valueOf(string));
                	
                    if (null != project) {
                        TblNbsjProjectDataEntity data = new TblNbsjProjectDataEntity();
                        data.setOldProjectId(project.getProjectId());
                        data.setProjectId(selectProject.getProjectId());
//                        this.tblNbsjProjectDataService.save(data);
                        
                        this.tblNbsjProjectDataMapper.saveOld(data);
                    }
                }
                return ResponseFormat.retParam(1,200,resultMap);
            }
            return ResponseFormat.retParam(0,30001,resultMap);
//            return JsonBean.error("未找到实施项目");
        }
		return ResponseFormat.retParam(0,30001,resultMap);
//        return JsonBean.error("导入失败");
        
	}
	public JsonBean getGkProjectInfo(String token, Integer pageNumber, Integer pageSize,TblGkProjectVo project) throws Exception {
    	
    	TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	project.setORGID(loginStaff.getCurrentOrg().getOrgid().toString());
    	PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>();
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
    	pageInfo.setTlist(tblNbsjProjectMapper.getGkProjectInfo(pageInfo,project));
    	pageInfo.setTotalRecord(tblNbsjProjectMapper.getGkProjectInfoListCount(project));
    	pageInfo.getTotalPage();
    	resultMap.put("pageInfo", pageInfo);
    	return ResponseFormat.retParam(1,200,resultMap);
	}
	public JsonBean getGkQuestionInfo(String token, Integer pageNumber, Integer pageSize,TblGkQuestionVo question) throws Exception {
		
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		if(pageNumber == null) {
			pageNumber = 1;
		}
		if(pageSize==null) {
			pageSize=15;
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		question.setORGID(loginStaff.getCurrentOrg().getOrgid().toString());
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>();
		pageInfo.setPageSize(pageSize);
		pageInfo.setCurrentPage(pageNumber);
		pageInfo.setTlist(tblNbsjProjectMapper.getGkQuestionInfoList(pageInfo,question));
		pageInfo.setTotalRecord(tblNbsjProjectMapper.getGkQuestionInfoListCount(question));
		pageInfo.getTotalPage();
		resultMap.put("pageInfo", pageInfo);
		return ResponseFormat.retParam(1,200,resultMap);
	}
	public JsonBean getGkZgContentInfo(String token, Integer pageNumber, Integer pageSize,TblGkZgQuestionVo question) throws Exception {
		
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		if(pageNumber == null) {
			pageNumber = 1;
		}
		if(pageSize==null) {
			pageSize=15;
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		question.setORGID(loginStaff.getCurrentOrg().getOrgid().toString());
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>();
		pageInfo.setPageSize(pageSize);
		pageInfo.setCurrentPage(pageNumber);
		pageInfo.setTlist(tblNbsjProjectMapper.getGkZgContentInfoList(pageInfo,question));
		pageInfo.setTotalRecord(tblNbsjProjectMapper.getGkZgContentInfoListCount(question));
		pageInfo.getTotalPage();
		resultMap.put("pageInfo", pageInfo);
		return ResponseFormat.retParam(1,200,resultMap);
	}
	
	
	
	
	
	
	
	private TblNbsjProject xmlbUpdate(TblNbsjProject project,String token,String pmid,String enddate,String startdate,String tempzyid,
			String tempid,String protempid,String pd_dx,String belongsto,String plan,String attids,String pjTeamJson ) throws Exception {
		
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return null;
		}
		
		TblOrganization organization1 = new TblOrganization();
		organization1.setOrgid(loginStaff.getCurrentOrg().getOrgid());
        
        if (project.getProjectId() != null) {
			// 修改开始
            TblNbsjProject tblnbsjProject = this.tblNbsjProjectMapper.selectPJById(project.getProjectId());//tblnbsjProjectService.getId(project.getProjectid().toString());
            tblnbsjProject.setAuditType(project.getAuditType());
            tblnbsjProject.setPlanYear(project.getPlanYear());
            tblnbsjProject.setCosts(project.getCosts());
            tblnbsjProject.setPrjoectName(project.getPrjoectName());
            tblnbsjProject.setProjectSource(project.getProjectSource());
            tblnbsjProject.setPurpose(project.getPurpose());
            tblnbsjProject.setScopes(project.getScopes());
            tblnbsjProject.setPursuant(project.getPursuant());
            tblnbsjProject.setComments(project.getComments());
            tblnbsjProject.setProDesc(project.getProDesc());
            tblnbsjProject.setProSjfs(project.getProSjfs());
            if(StringUtils.isNotEmpty(tempzyid)){
//                TblNbsjTempleteEntity templetezy = tblNbsjTempleteService.get(new BigDecimal(tempzyid));//指引模板
                TblNbsjTempleteEntity templetezy = tblNbsjTempleteMapper.findbyid(tempzyid);//指引模板
                tblnbsjProject.setTbltempletezy(templetezy);
            }
            tblnbsjProject.setCreateTime(new Date());
            tblnbsjProject.setUpdateStatus(TblNbsjProject.UPDATEYES);
            if(tblnbsjProject.getExamineType()!=null){tblnbsjProject.setExamineType(project.getExamineType());}

            //examinetype
            tblnbsjProject.setPmId(Integer.parseInt(pmid));
            /*主审人
            String umpireid = request.getParameter("umpireid");
            TblStaff um = new TblStaff();
            um.setStaffid(new BigDecimal(umpireid));*/
            /*质控经理
            String controlid = request.getParameter("controlid");
            TblStaff con = new TblStaff();
            con.setStaffid(new BigDecimal(controlid));*/
            //tblnbsjProject.setTblcontrol(con);
            //tblnbsjProject.setTblumpe(um);
			//修改审计模板
			if (StringUtils.isNotEmpty(tempid)) { 
				// 如果审计模板再次修改
				//if(!tempid.equals(protempid)){
					TblNbsjTempleteEntity templete = tblNbsjTempleteMapper.findbyid(tempid);//tblNbsjTempleteService.get(new BigDecimal(tempid));
					TblNbsjTempleteEntity temp = new TblNbsjTempleteEntity();
					temp.setTempType("3");
					temp.setCreateDate(templete.getCreateDate());
					temp.setTempleteCode(templete.getTempleteCode());
					temp.setTempleteDesc(templete.getTempleteDesc());
					Set<TblOrganization> orgs = templete.getOrganizations();
					if (orgs != null && orgs.size() > 0) {
						for (TblOrganization org : orgs) {
							temp.getOrganizations().add(org);
						}
					}
					temp.setStaffId(templete.getStaffId());
					temp.setTempleteType(templete.getTempleteType());
					temp.setUpdateStaffId(templete.getUpdateStaffId());
					temp.setUpdateDate(templete.getUpdateDate());
					temp.setStatus(templete.getStatus());
					temp.setTempleteName(templete.getTempleteName());
//					tblNbsjTempleteService.save(temp);
					tblNbsjTempleteMapper.insertEntity(temp);
					
					tblnbsjProject.setTempId(temp.getTempleteId());
					tblnbsjProject.setOrgInfo(organization1);
					// if(!tempid.equals(tblnbsjProject.getTbltemplete().getTempleteId().toString())){
					List<TblTargetTypeEntity> list = tblTargetTypeService.findByAllMB(tempid);
					if (list != null && list.size() > 0) {
						for (TblTargetTypeEntity tblTargetType : list) {
							TblTargetTypeEntity target = new TblTargetTypeEntity();
							target.setCreateTime(tblTargetType.getCreateTime());
							target.setNbsjTemplete(temp);
							target.setParentId(tblTargetType.getParentId());
							target.setStatus(TblTargetTypeEntity.TEMP_NUMBER);//TEMP_NUMBER 0 ;ZY_NUMBER 1
							target.setTargetDesc(tblTargetType.getTargetDesc());
							target.setTargetName(tblTargetType.getTargetName());
//							tblTargetTypeService.save(target);
							tblTargetTypeMapper.insertEntity(target);
							List<TblAduitProGramEntity> grams = tblAduitProGramService.findByALL(tblTargetType.getTargetId().toString());
							if (grams != null && grams.size() > 0) {
								for (TblAduitProGramEntity gram : grams) {
									TblAduitProGramEntity prog = new TblAduitProGramEntity();
									prog.setBioData(gram.getBioData());
									prog.setTargetId(target.getTargetId().intValue());
									prog.setControl(gram.getControl());
									prog.setBusinessType(gram.getBusinessType());
									prog.setStatus(TblAduitProGramEntity.TEMP_NUMBER);
									prog.setCreateTime(gram.getCreateTime());
									prog.setNbsjTemplete(temp);
									prog.setTempId(temp.getTempleteId());
									prog.setRiskPoint(gram.getRiskPoint());
									prog.setRiskSource(gram.getRiskSource());
									prog.setSuditProcess(gram.getSuditProcess());
									prog.setUpdateTime(gram.getUpdateTime());
//									tblAduitProGramService.save(prog);
									tblAduitProGramMapper.insertEntity(prog);
								}
							}
							capyMb(tblTargetType.getTargetId().toString(), temp, target.getTargetId().toString());
						}
					}
					//删除之前分配的任务
					//删除任务
					
						 List<TblNbsjAuthorizationEntity> nbsjauthorizatoin = this.tblNbsjAuthorizationService.getByProjectId(project.getProjectId());
						 if(null!=nbsjauthorizatoin&&nbsjauthorizatoin.size()>0) {
							 for (TblNbsjAuthorizationEntity tblNbsjAuthorization : nbsjauthorizatoin) {
								 if(null!=nbsjauthorizatoin) {
									 List<TblNbsjOperateEntity> listoper = tblNbsjOperateMapper.findByAuthId(tblNbsjAuthorization.getAuthId());//tblNbsjOperateService.findByAuthId(tblNbsjAuthorization.getAuthId());
									 TblNbsjOperateEntity oper = null;
									 if(listoper!=null && listoper.size()>0){
										 oper = listoper.get(0);
										}
									 tblNbsjOperateMapper.deleteByOpid(oper.getOperateid());
//									 tblNbsjOperateService.delete(oper);
//									 this.tblNbsjAuthorizationService.delete(tblNbsjAuthorization);
									 this.tblNbsjAuthorizationMapper.deleteByAuthid(tblNbsjAuthorization.getAuthId());
								 }
							 }
						 }
					//一是已经是在项目里的，已经复制出来的，可以删除
					//二是没在项目里的，没复制出来的，不能删除
					if(StringUtils.isNotEmpty(protempid)) {
						tblAduitProGramService.deleteZy(protempid);
						tblTargetTypeService.deleteByZY(protempid);
						tblNbsjTempleteService.delete(new BigDecimal(protempid));
					}
				//}
			}
            try {
                if (startdate != null && startdate.length() > 0) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    tblnbsjProject.setStartDate(sdf.parse(startdate));
                }
                if (enddate != null && enddate.length() > 0) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    tblnbsjProject.setEndDate(sdf.parse(enddate));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //项目对象
//            String pd_dx = request.getParameter("pd_dx");
//            String org = request.getParameter("belongsto");
            if (StringUtils.isNotEmpty(pd_dx) && pd_dx.equals("yh")){
//                TblStaff staff = tblStaffMapper.getById(belongsto);//tblStaffNewService.get(new BigDecimal(belongsto));
                tblnbsjProject.setAuditStaffId(Integer.valueOf(belongsto));//.setTblnbsjstaffs(staff);
//                TblOrganization o = organizationService.findById(staff.getTblOrganization().getOrgid().toString());
//                tblnbsjProject.setTblnbsjorgs(null);
//                //tblnbsjProject.setTblnbsjorgs(o);
                tblnbsjProject.setAuditOrgId(null);
            }else {
            	tblnbsjProject.setAuditOrgId(Integer.valueOf(belongsto));
//                TblOrganization organization = organizationService.findById(org);
//                tblnbsjProject.setTblnbsjorgs(organization);
//                tblnbsjProject.setTblnbsjstaffs(null);
            	tblnbsjProject.setAuditStaffId(null);
            }
            //被审计单位
			//String belongsto = request.getParameter("belongsto");
			//String belongstoText = request.getParameter("belongstoText");
			//project.setOrgids(belongsto);
			//project.setOrgidNames(belongstoText);
			
//            String plan = request.getParameter("plan");
            Integer planid = Integer.valueOf(plan);
            TblNbsjAuditplan jplan = tblNbsjAuditplanMapper.selectNbsjAuditPlanEntityById(planid);//tblnbsjauditPlanService.get(plan);
            tblnbsjProject.setTblnbsjPlan(jplan);
            
//            tblnbsjProjectService.update(tblnbsjProject);
            tblNbsjProjectMapper.updateEntity(tblnbsjProject);
            
            Integer projectId = project.getProjectId();
            
          //保存附件
            this.tblAttachmentMapper.deleteAttmentRelationProject(projectId);
            if (StringUtils.isNotBlank(attids)){
            	String[] ids = attids.split(",");
            	for (int i = 0; i < ids.length; i++) {
//            		TblAttachment att = attachmentService.findById(ids[i].trim());
            		String id = ids[i].trim();
					//TblAttachment att = this.tblAttachmentMapper.selectEntityById(attId);
//            		project.getTblprojectAtts().add(att);
        			this.tblAttachmentMapper.insertAttmentRelationProject(id, projectId);
            	}
            }
            
            //==0.0
//            this.project_team_save(request, project.getProjectid().toString());
            //==项目小组
    		if(null != pjTeamJson && !"".equals(pjTeamJson.trim())) {
    			List<TblNbsjProjectTeamEntity> srList = new ArrayList<TblNbsjProjectTeamEntity>();
    			srList = JSONObject.parseArray(pjTeamJson,TblNbsjProjectTeamEntity.class);
    			for (TblNbsjProjectTeamEntity pjTeam : srList) {
    				//
    				String zystaffids = pjTeam.getZystaffids();
    				Integer leaderid = pjTeam.getLeaderId();
//    				Integer projectid = pjTeam.getProjectid();
					this.tblNbsjProjectteamService.pjItemAdd(token,pjTeam,zystaffids,leaderid,projectId);
    			}
    		}
            
            return tblnbsjProject;
        }else {
        	//新建项目
            try {
                if (startdate != null && startdate.length() > 0) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    project.setStartDate(sdf.parse(startdate));
                }
                if (enddate != null && enddate.length() > 0) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    project.setEndDate(sdf.parse(enddate));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            
            Integer planid = Integer.valueOf(plan);
            TblNbsjAuditplan jplan = tblNbsjAuditplanMapper.selectNbsjAuditPlanEntityById(planid);//tblnbsjauditPlanService.get(plan);
            project.setTblnbsjPlan(jplan);
            TblStaff pm = new TblStaff();
            pm.setStaffid(new BigDecimal(pmid));
            project.setPmStaff(pm);//.setTblPm(pm);
            project.setOrgInfo(organization1);//.setTblorg(organization1);
//            if (tempid != null) {
			//第一次新增审计模板
			if (StringUtils.isNotEmpty(tempid)) {
				//选择的方案模板(来自方案模板)
//				TblNbsjTempleteEntity templete = tblNbsjTempleteService.get(new BigDecimal(tempid));
				TblNbsjTempleteEntity templete = tblNbsjTempleteMapper.findbyid(tempid);
				//复制审计模板
				TblNbsjTempleteEntity temp = new TblNbsjTempleteEntity();
				temp.setTempType("3");
				temp.setCreateDate(templete.getCreateDate());
				temp.setTempleteCode(templete.getTempleteCode());
				temp.setTempleteDesc(templete.getTempleteDesc());
				Set<TblOrganization> orgs = templete.getOrganizations();
				if (orgs != null && orgs.size() > 0) {
					for (TblOrganization org : orgs) {
						temp.getOrganizations().add(org);
					}
				}
				temp.setStaffId(templete.getStaffId());
				temp.setTempleteType(templete.getTempleteType());
				temp.setUpdateStaffId(templete.getUpdateStaffId());
				temp.setUpdateDate(templete.getUpdateDate());
				temp.setStatus(templete.getStatus());
				temp.setTempleteName(templete.getTempleteName());
//				tblNbsjTempleteService.save(temp);
				tblNbsjTempleteMapper.insertEntity(temp);
				
				project.setTempId(temp.getTempleteId());
				if (StringUtils.isNotEmpty(tempzyid)) {
//					TblNbsjTempleteEntity templetezy = tblNbsjTempleteService.get(new BigDecimal(tempzyid));
					TblNbsjTempleteEntity templetezy = tblNbsjTempleteMapper.findbyid(tempzyid);
					project.setTbltempletezy(templetezy);
				}

				List<TblTargetTypeEntity> list = tblTargetTypeService.findByAllMB(tempid);
				if (list != null && list.size() > 0) {
					for (TblTargetTypeEntity tblTargetType : list) {
						TblTargetTypeEntity target = new TblTargetTypeEntity();
						target.setCreateTime(tblTargetType.getCreateTime());
						target.setNbsjTemplete(temp);
						target.setParentId(tblTargetType.getParentId());
						target.setStatus(TblTargetTypeEntity.TEMP_NUMBER);
						target.setTargetDesc(tblTargetType.getTargetDesc());
						target.setTargetName(tblTargetType.getTargetName());
//						tblTargetTypeService.save(target);
						tblTargetTypeMapper.insertEntity(target);
						List<TblAduitProGramEntity> grams = tblAduitProGramService.findByALL(tblTargetType.getTargetId().toString());
						if (grams != null && grams.size() > 0) {
							for (TblAduitProGramEntity gram : grams) {
								TblAduitProGramEntity prog = new TblAduitProGramEntity();
								prog.setBioData(gram.getBioData());
								prog.setTargetId(target.getTargetId().intValue());
								prog.setControl(gram.getControl());
								prog.setBusinessType(gram.getBusinessType());
								prog.setStatus(TblAduitProGramEntity.TEMP_NUMBER);
								prog.setCreateTime(gram.getCreateTime());
								prog.setTempId(temp.getTempleteId());
								prog.setRiskPoint(gram.getRiskPoint());
								prog.setRiskSource(gram.getRiskSource());
								prog.setSuditProcess(gram.getSuditProcess());
								prog.setUpdateTime(gram.getUpdateTime());
//								tblAduitProGramService.save(prog);
								tblAduitProGramMapper.insertEntity(prog);
							}
						}
						capyMb(tblTargetType.getTargetId().toString(), temp, target.getTargetId().toString());
					}
				}
			}
            //项目对象
//            String pd_dx = request.getParameter("pd_dx");
//            String org = request.getParameter("belongsto");
//            if (StringUtils.isNotEmpty(pd_dx) && pd_dx.equals("yh")){
//                TblStaff staff = tblStaffNewService.get(new BigDecimal(org));
//                project.setTblnbsjstaffs(staff);
//                TblOrganization o = organizationService.findById(staff.getTblOrganization().getOrgid().toString());
//                project.setTblnbsjorgs(null);
//            }else {
//                TblOrganization organization = organizationService.findById(org);
//                project.setTblnbsjorgs(organization);
//                project.setTblnbsjstaffs(null);
//            }
			if (StringUtils.isNotEmpty(pd_dx) && pd_dx.equals("yh")){
				project.setAuditStaffId(Integer.valueOf(belongsto));//.setTblnbsjstaffs(staff);
	          }else {
	        	  project.setAuditOrgId(Integer.valueOf(belongsto));
	          }
			
            //被审计单位
            project.setStatus(0);
            project.setFpStatus(0);//项目任务分配状态
            project.setAssigbedControlTime(new Date());
            project.setAssigbedumpeTime(new Date());
            project.setAssigbedpmTime(new Date());
            project.setCreateTime(new Date());
            project.setUpdateStatus(TblNbsjProject.UPDATEYES);
//            TblStaff user = (TblStaff) request.getSession().getAttribute("longUser");
            project.setCreateStaffId(loginStaff.getStaffid().intValue());//setTblcreater(user);
            project.setCyrrentStatre(TblNbsjProject.NO_SELECT);
            project.setOrgId(loginStaff.getCurrentOrg().getOrgid().intValue());
            
//            tblnbsjProjectService.save(project);
            tblNbsjProjectMapper.insertEntity(project);
            
            Integer projectId = project.getProjectId();
            
          //保存附件
            if (StringUtils.isNotBlank(attids)){
            	String[] ids = attids.split(",");
            	for (int i = 0; i < ids.length; i++) {
//            		TblAttachment att = attachmentService.findById(ids[i].trim());
            		String id = ids[i].trim();
//            		TblAttachment att = this.tblAttachmentMapper.selectEntityById(attId);
//            		project.getTblprojectAtts().add(att);
        			this.tblAttachmentMapper.insertAttmentRelationProject(id, projectId);
            	}
            }
            
            //审计小组
//            this.project_team_save(request, project.getProjectid().toString());
            if(null != pjTeamJson && !"".equals(pjTeamJson.trim())) {
    			List<TblNbsjProjectTeamEntity> srList = new ArrayList<TblNbsjProjectTeamEntity>();
    			srList = JSONObject.parseArray(pjTeamJson,TblNbsjProjectTeamEntity.class);
    			for (TblNbsjProjectTeamEntity pjTeam : srList) {
    				//
    				String zystaffids = pjTeam.getZystaffids();
    				Integer leaderid = pjTeam.getLeaderId();
    				//pjTeam.getProjectid();
					this.tblNbsjProjectteamService.pjItemAdd(token,pjTeam,zystaffids,leaderid,projectId);
    			}
    		}
            
            return project;
        }
    }
	
	public void capyMb(String parentid, TblNbsjTempleteEntity temp, String newparentid) throws Exception {
		List<TblTargetTypeEntity> list = tblTargetTypeService.findByALLPatrnt(parentid);
		if (list != null && list.size() > 0) {
			for (TblTargetTypeEntity tblTargetType : list) {
				TblTargetTypeEntity target = new TblTargetTypeEntity();
				target.setCreateTime(tblTargetType.getCreateTime());
				target.setNbsjTemplete(temp);
				target.setParentId(new BigDecimal(newparentid));
				target.setStatus(TblTargetTypeEntity.ZY_NUMBER);
				target.setTargetDesc(tblTargetType.getTargetDesc());
				target.setTargetName(tblTargetType.getTargetName());
//				tblTargetTypeService.save(target);
				tblTargetTypeMapper.insertEntity(target);
				List<TblAduitProGramEntity> grams = tblAduitProGramService.findByALL(tblTargetType.getTargetId().toString());
				if (grams != null && grams.size() > 0) {
					for (TblAduitProGramEntity gram : grams) {
						TblAduitProGramEntity prog = new TblAduitProGramEntity();
						prog.setBioData(gram.getBioData());
						prog.setTargetId(target.getTargetId().intValue());
						prog.setBusinessType(gram.getBusinessType());
						prog.setStatus(TblAduitProGramEntity.ZY_NUMBER);
						prog.setCreateTime(gram.getCreateTime());
						prog.setControl(gram.getControl());
						//prog.setIsUseProgram(0);
						prog.setTempId(temp.getTempleteId());
						prog.setRiskPoint(gram.getRiskPoint());
						prog.setRiskSource(gram.getRiskSource());
						prog.setSuditProcess(gram.getSuditProcess());
						prog.setUpdateTime(gram.getUpdateTime());
//						tblAduitProGramService.save(prog);
						tblAduitProGramMapper.insertEntity(prog);
					}
				}
				capyMb(tblTargetType.getTargetId().toString(), temp, target.getTargetId().toString());
			}
		}
	}
	
	
	@Override
	public R removeAttInfoByAttId(String token, String attId) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return R.fail("用户已失效！");
		}
        return this.deleteRealtionAttInfo(attId);
	}

	private R deleteRealtionAttInfo(String attId) throws Exception {
		boolean b = false;
        TblAttachment att = this.tblAttachmentMapper.selectEntityById(attId);
        this.tblNbsjProjectMapper.deleteFileInfoByAttId(att.getAttid().intValue());
        this.tblAttachmentMapper.deleteEntity(att.getAttid());
        
        return R.success();
	}




	@Override
	public JsonBean getTempeleBizCnt(String token, Integer templeteId) throws Exception {
		
		
		PageInfo<TblAduitProGramEntity> pageInfo = new PageInfo<TblAduitProGramEntity>();
    	pageInfo.setPageSize(15);
    	pageInfo.setCurrentPage(1);
		
		Integer cnt = this.tblAduitProGramMapper.selectCountByPageInfo(pageInfo,null,templeteId,null);
		
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	resultMap.put("cnt", cnt);
    	return ResponseFormat.retParam(1,200,resultMap);
	}
	
}
